package com.general.framework.utils;

import java.io.*;

public class DeepCloneUtil {

    public static <T extends Serializable> T deepClone(T obj) throws IOException, ClassNotFoundException {
        if (obj == null) {
            return null;
        }
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.close();

            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            T clone = (T) ois.readObject();
            ois.close();
            return clone;
        } catch (NotSerializableException e) {
            e.printStackTrace();
        }
        return obj;
    }
}